#include "../include/client_io.h"

void printPrompt()
{
	printf("+----- 交易平台\n");
	printf("+----- 按1登陆\n");
	printf("+----- 按2购买商品\n");
	printf("+----- 按3出售商品\n");
	printf("+----- 按4查看商品信息\n");
    printf("+----- 按5列出所有商品\n");
    printf("+----- 按6列出买卖记录\n");
    printf("+----- 按7登出\n");
    printf("+----- 按0退出\n");
	fflush(stdout);
}


int Fgets(char *str, int n) {
    if (fgets(str, n, stdin) == NULL) {
		perror("输入错误\n");
	    return -1;
	}

    int len;
	if ((len = strlen(str)) <= 1) {
		return -2;
	}

    if (str[len - 1] == '\n') {
        str[len - 1] = '\0';
    }
	
    return 0;
}

int ReadInt(int *value) {
    char buffer[100];
    
    if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
		perror("输入错误");
        return -1; 
    }
    
    char *endptr;
    *value = (int)strtol(buffer, &endptr, 10);
    
    if (endptr == buffer || (*endptr != '\0' && !isspace((unsigned char)*endptr))) {
        return -2;
    }
    
    return 0;
}

char* edit_with_vim_and_get_content() {

    char temp_filename[] = "/tmp/vim_edit_XXXXXX";
    int fd = mkstemp(temp_filename);
    if (fd == -1) {
        perror("创建临时文件失败");
        return NULL;
    }
    close(fd);
    
    char command[256];
    snprintf(command, sizeof(command), "vim +startinsert %s", temp_filename);
    int ret = system(command);
    if (ret != 0) {
        fprintf(stderr, "vim 编辑失败\n");
        unlink(temp_filename);
        return NULL;
    }
    
    FILE* file = fopen(temp_filename, "r");
    if (!file) {
        perror("打开临时文件失败");
        unlink(temp_filename);
        return NULL;
    }
    
    fseek(file, 0, SEEK_END);
    long file_size = ftell(file);
    fseek(file, 0, SEEK_SET);
    
    if (file_size <= 0) {
        fclose(file);
        unlink(temp_filename);
        return strdup("");
    }
    
    static char content[DESCRIPTION_SIZE + 1];
    memset(content, 0x00, DESCRIPTION_SIZE);
    size_t read_size = file_size > DESCRIPTION_SIZE ? DESCRIPTION_SIZE : file_size;

    size_t bytes_read = fread(content, 1, read_size, file);
    content[bytes_read] = '\0';
    
    fclose(file);
    unlink(temp_filename);
    
    return content;
}

int ReadText(char *str, int n) {
    if (strncpy(str, edit_with_vim_and_get_content(), n) == NULL) {
        perror("输入错误\n");
	    return -1;
    }
    int len;
	if ((len = strlen(str)) <= 1) {
		return -1;
	}
    if (str[len - 1] == '\n') {
        str[len - 1] = '\0';
    }
    return 0;
}


void format_print(char *message) {
	printf("\033[2K\r\033[34m%s\033[0m", message);
	fflush(stdout);
}

void format_err(char *message) {
	printf("\033[2K\r\033[31m%s\033[0m", message);
	fflush(stdout);
}


int get_buy_args(Command *cmd) {
	cmd->type = BUY;

	format_print("请输入商品ID:\n");
	if (ReadInt(&cmd->good_id) < 0) {
		return -1;
	}
	format_print("请输入数量:\n");
	if (ReadInt(&cmd->number) < 0) {
		return -1;
	}
	return 1;
}

int get_login_args(Command *cmd) {
	cmd->type = LOGIN;
	
	format_print("请输入用户名:\n");
	if (Fgets(cmd->name, 63) < 0) {
		return -1;
	}
	return 1;
}

int get_sell_args(Command *cmd) {
	cmd->type = SELL;

	format_print("请输入商品名:\n");
	if (Fgets(cmd->name, NAME_SIZE) < 0) {
		return -1;
	}
	format_print("请输入数量:\n");
	if (ReadInt(&cmd->number) < 0) {
		return -1;
	}
	format_print("请输入价格:\n");
	if (ReadInt((int *)&cmd->price) < 0) {
		return -1;
	}
	format_print("请输入商品描述:\n(按任意键开始编辑)");
	getchar();
	if (ReadText(cmd->description, MAXSIZE) < 0) {
		return -1;
	}
	return 1;
}

int get_view_args(Command *cmd) {
	cmd->type = VIEW;

	format_print("请输入商品ID:\n");
	if (ReadInt(&cmd->good_id) < 0) {
		return -1;
	}
	return 1;
}